package com.ufenlei.alarm
{
	import flash.desktop.NotificationType;
	import flash.display.*;
	import flash.events.EventDispatcher;
	import flash.geom.Rectangle;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.utils.setTimeout;
	
	import mx.modules.ModuleLoader;
	
	import view.RemiderWindow;
	
	
	/**
	 * 定时器提醒管理
	 **/
	public class TimerRemindManager extends EventDispatcher
	{
		protected var notifyWin:RemiderWindow;
		
		[Embed(source="assert/sound.mp3")]
		private var AlarmSound:Class;
		
		private var _notifyWindowPosition:String;
		
		private var sound:Sound;
		private var sc:SoundChannel;
		
		private var ml:ModuleLoader = new ModuleLoader();
		protected var notifyList:Array = new Array();
		
		public function TimerRemindManager()
		{	
			sound = new AlarmSound() as Sound;
			setTimeout(init,2000);
		}
		public function notify(a:Alarm):void{
			this.playSound();			
			this.dispatchEvent(new RemindEvent(RemindEvent.REMIND_CHANG));
			if(Config.getInstance().notifyWindow){
				this.notifyList.push(a);
				this.showNotifyWindow()
			};	
		}
		private function init():void{
			//初始化窗口
			this.notifyWin = new RemiderWindow();
			notifyWin.visible = false;
			this.notifyWin.open();
			
			//初始化事件
			notifyWin.addEventListener( RemindEvent.REMIND_CLOSE,onNotifyWindowClose);
		}
		private function setPosition():void{
			var m:Boolean = false;
			if(this._notifyWindowPosition == null){
				this._notifyWindowPosition = Config.getInstance().notifyWindowPosition;
				m = true;
			}else if(this._notifyWindowPosition != Config.getInstance().notifyWindowPosition){
				this._notifyWindowPosition = Config.getInstance().notifyWindowPosition;
				m = true;
			}
			if(m){
				var screen:Screen = Screen.mainScreen;
				var sr:Rectangle = screen.bounds;
				var winr:Rectangle = this.notifyWin.nativeWindow.bounds;
				var bounds:Rectangle;
				switch(this._notifyWindowPosition){
					case "rt":
						bounds = new Rectangle(sr.right - winr.width - 5,sr.top + 5,winr.width,winr.height);
						break;
					case "rb":
						bounds = new Rectangle(sr.right - winr.width - 5,sr.bottom - winr.height - 5,
											   winr.width,winr.height);
						break;
					case "center":
						bounds = new Rectangle((sr.width - winr.width)/2,(sr.height-winr.height)/2,
						                       winr.width,winr.height);
						break;
					default:
						bounds = new Rectangle(sr.right - winr.width - 5,sr.top + 5,
								               winr.width,winr.height);
				}
				this.notifyWin.nativeWindow.bounds = bounds;
			}
			
		}
		private function onNotifyWindowClose(event:RemindEvent):void{
			this.notifyWin.visible = false;
			this.notifyList.shift();
			this.dispatchEvent(new RemindEvent(RemindEvent.REMIND_CHANG));
			this.showNotifyWindow();
		}
		public function playSound():void{
			stopPlaySound();
			this.sc = sound.play(0,3);
			var tr:SoundTransform = sc.soundTransform;
			tr.volume = Config.getInstance().volume;
			sc.soundTransform = tr;
		}
		private function showNotifyWindow():void{
			if(!this.notifyWin||this.notifyWin.closed) {
				init();
				setTimeout(showNotifyWindow,50);
				return;
			}
			this.setPosition();
			if(!this.notifyWin.visible && this.notifyList.length>0){
				this.notifyWin.alarm = this.notifyList[0] as Alarm;
				this.notifyWin.open();
				this.notifyWin.visible = true;
				this.notifyWin.nativeWindow.notifyUser(NotificationType.CRITICAL);
			}
		}
		public function stopPlaySound():void{
			if (this.sc!= null) {
				sc.stop();
				sc = null;
			}
		}
		override public function toString():String{
			return (this.notifyList.length>0)?(this.notifyList[0] as Alarm).title+" {"+this.notifyList.length + " }":
					"null";
		}
		public function get hasNotify():Boolean{
			return this.notifyList.length > 0;
		}
		public function close():void{
			if(this.notifyWin)this.notifyWin.close();
		}
	}
}